package com.library.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.service.AdminService;
import com.library.service.impl.AdminServiceImpl;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	
//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		AdminService adminService = new AdminServiceImpl();
//		String account = req.getParameter("account");
//		String password = req.getParameter("password");
//		Map<String,Object> map = adminService.login(account, password);
//		if(map.get("loginAdmin") != null) {
//			HttpSession session=req.getSession();
//			session.setAttribute("loginAdmin",map.get("loginAdmin"));
//			resp.sendRedirect("book.jsp");
//		}else {
//			req.setAttribute("msg", map.get("msg"));
//			req.getRequestDispatcher("login.jsp").forward(req, resp);
//		}
//	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    AdminService adminService = new AdminServiceImpl();
	    String account = req.getParameter("account");
	    String password = req.getParameter("password");
	    Map<String,Object> map = adminService.login(account, password);
	    if(map.get("loginAdmin") != null) {
	        HttpSession session = req.getSession();
	        session.setAttribute("loginAdmin", map.get("loginAdmin"));
	        // 重定向到获取图书的Servlet，而不是直接跳转到JSP
	        resp.sendRedirect("getBook.do?pageNum=1&pageSize=5"); // 设置合理的默认分页参数
	    } else {
	        req.setAttribute("msg", map.get("msg"));
	        req.getRequestDispatcher("login.jsp").forward(req, resp);
	    }
	}
}
